/*
 ============================================================================
 Name        : Set_CGI.c
 Author      : liuzhanchun
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#if 1
#define SERVER_NAME cgiServerName
#endif
#if 0
#define SERVER_NAME "www.boutell.com"
#endif

/* You may need to change this, particularly under Windows;
	it is a reasonable guess as to an acceptable place to
	store a saved environment in order to test that feature.
	If that feature is not important to you, you needn't
	concern yourself with this. */

#ifdef WIN32
#define SAVED_ENVIRONMENT "c:\\cgicsave.env"
#else
#define SAVED_ENVIRONMENT "/tmp/cgicsave.env"
#endif /* WIN32 */

#include <stdio.h>
#include "cgic.h"
#include <string.h>
#include <stdlib.h>
#include "config_ini.h"
#include "axi_lite.h"



#define SET_AXI_camera_line_number         0x00
#define SET_AXI_whitebalance               0x01

#define SET_AXI_image_rectification        0x02    //图像矫正
#define SET_AXI_camera_fusion_degree       0x03     //图像融合
#define SET_AXI_camera_brightness          0x04    //图像亮度
#define SET_AXI_triggerinput_1             0x05    //触发输入  第一组
#define SET_AXI_triggerinput_2             0x06    //触发输入  第二组
#define SET_AXI_camera_delay               0x07
#define SET_AXI_HDMI                       0x08
#define SET_AXI_marking_setting_h          0x09
#define SET_AXI_marking_setting            0x0A
#define SET_AXI_marking_number             0x0B
#define GET_FPGA_VERSION                   0x0D

int nCamera_line_number;
int nWhitebalance;
int nCamera_fusion_degree;
int nCamera_brightness;
int nCcamera_delay;
int nCamera_HDMI;
int nTriggerinput_1;
int nTriggerinput_2;
int nMarking_setting;
int nMarking_number;
int nimage_rectification;


#define board

//像机设置
int cgiMain() {

	//cgiFormResultType cgi_form_result;

#ifdef board
	AXI_Lite_Init();
#endif

    /*从html获取数据，设置cookie*/
    char camera_line_number[32] = {0};
	cgiFormString("camera_line_number", camera_line_number, 32);
	cgiFormInteger("camera_line_number",&nCamera_line_number,0);
	fprintf(cgiOut,"Set-Cookie: %s=%s;  path=/\r\n","camera_line_number",camera_line_number);

	char whitebalance[32] = {0};
	cgiFormString("whitebalance", whitebalance, 32);
	fprintf(cgiOut,"Set-Cookie: %s=%s;  path=/\r\n","whitebalance",whitebalance);

	
	char camera_fusion_degree[32] = {0};
	cgiFormString("camera_fusion_degree", camera_fusion_degree, 32);
	cgiFormInteger("camera_fusion_degree",&nCamera_fusion_degree,0);
	fprintf(cgiOut,"Set-Cookie: %s=%s;  path=/\r\n","camera_fusion_degree",camera_fusion_degree);

	char fisheyeCorrection[32] = {0};
	cgiFormString("fisheyeCorrection", fisheyeCorrection, 32);
	fprintf(cgiOut,"Set-Cookie: %s=%s;  path=/\r\n","fisheyeCorrection",fisheyeCorrection);	

	if(strcmp(fisheyeCorrection,"open_fisheyeCorrection") == 0) {
		nimage_rectification = 1;
		//nCamera_fusion_degree = (0x0001 << 16) | (nCamera_fusion_degree&0x0000ffff);
	} else {
		//nCamera_fusion_degree = (0x0000 << 16) | (nCamera_fusion_degree&0x0000ffff);
		nimage_rectification = 0;
	}


    char camera_brightness[32] = {0};
	cgiFormString("camera_brightness", camera_brightness, 32);
	cgiFormInteger("camera_brightness",&nCamera_brightness,0);
    fprintf(cgiOut,"Set-Cookie: %s=%s;  path=/\r\n","camera_brightness",camera_brightness);


	char camera_delay[32] = {0};
	cgiFormString("camera_delay", camera_delay, 32);
	cgiFormInteger("camera_delay",&nCcamera_delay,0);
    fprintf(cgiOut,"Set-Cookie: %s=%s;  path=/\r\n","camera_delay",camera_delay);

	char camera_HDMI_display[32] = {0};
	cgiFormString("camera_HDMI_display", camera_HDMI_display, 32);
	//cgiFormInteger("nCamera_HDMI",&nCamera_HDMI,0);
    fprintf(cgiOut,"Set-Cookie: %s=%s;  path=/\r\n","camera_HDMI_display",camera_HDMI_display);

	char camera_HDMI_display_switch[32] = {0};
	cgiFormString("camera_HDMI_display_switch", camera_HDMI_display_switch, 32);
	fprintf(cgiOut,"Set-Cookie: %s=%s;  path=/\r\n","camera_HDMI_display_switch",camera_HDMI_display_switch);

	char triggerinput_1[32] = {0};
	cgiFormString("triggerinput_1", triggerinput_1, 32);
	fprintf(cgiOut,"Set-Cookie: %s=%s;  path=/\r\n","triggerinput_1",triggerinput_1);

	char triggerinput_2[32] = {0};
	cgiFormString("triggerinput_2", triggerinput_2, 32);
	fprintf(cgiOut,"Set-Cookie: %s=%s;  path=/\r\n","triggerinput_2",triggerinput_2);

	char marking_setting[32] = {0};
	cgiFormString("marking_setting", marking_setting, 32);
	fprintf(cgiOut,"Set-Cookie: %s=%s;  path=/\r\n","marking_setting",marking_setting);

	char marking_number[32] = {0};
	cgiFormString("marking_number", marking_number, 32);
	cgiFormInteger("marking_number",&nMarking_number,0);
	fprintf(cgiOut,"Set-Cookie: %s=%s;  path=/\r\n","marking_number",marking_number);

	/* Send the content type, letting the browser know this is HTML */
	cgiHeaderContentType("text/html; charset=UTF-8");

	/* Top of the page */
	fprintf(cgiOut, "<HTML><HEAD>\n");
	fprintf(cgiOut, "<TITLE>web 管理</TITLE></HEAD>\n");
	fprintf(cgiOut, "<BODY><H1></H1>\n");


	//存储数据
	configFileStrSet("/boa/my.ini","camera_line_number",camera_line_number);
	configFileStrSet("/boa/my.ini","whitebalance",whitebalance);
	configFileStrSet("/boa/my.ini","camera_fusion_degree",camera_fusion_degree);
	configFileStrSet("/boa/my.ini","fisheyeCorrection",fisheyeCorrection);
	configFileStrSet("/boa/my.ini","camera_brightness",camera_brightness);
	configFileStrSet("/boa/my.ini","camera_delay",camera_delay);
	configFileStrSet("/boa/my.ini","camera_HDMI_display",camera_HDMI_display);
    configFileStrSet("/boa/my.ini","camera_HDMI_display_switch",camera_HDMI_display_switch);
	configFileStrSet("/boa/my.ini","triggerinput_1",triggerinput_1);
	configFileStrSet("/boa/my.ini","triggerinput_2",triggerinput_2);
	configFileStrSet("/boa/my.ini","marking_setting",marking_setting);
	configFileStrSet("/boa/my.ini","marking_number",marking_number);

	char *ret =  strstr(whitebalance,"close");
	if(ret != NULL)
	{
		//fprintf(cgiOut, "close = %s\n",whitebalance);
		nWhitebalance = 0;
	}
	else
	{
		//fprintf(cgiOut, "open = %s\n",whitebalance);
		nWhitebalance = 1;
	}

	ret =  strstr(triggerinput_1,"close");
	if(ret != NULL)
	{
		//fprintf(cgiOut, "close = %s\n",triggerinput_1);
		nTriggerinput_1 = 0;
	}
	else
	{
		//fprintf(cgiOut, "open = %s\n",triggerinput_1);
		nTriggerinput_1 = 1;
	}

	ret =  strstr(triggerinput_2,"close");
	if(ret != NULL)
	{
		//fprintf(cgiOut, "close = %s\n",triggerinput_2);
		nTriggerinput_2 = 0;
	}
	else
	{
		//fprintf(cgiOut, "open = %s\n",triggerinput_2);
		nTriggerinput_2 = 1;
	}


	ret =  strstr(marking_setting,"close");
	if(ret != NULL)
	{
		//fprintf(cgiOut, "close = %s\n",marking_setting);
		nMarking_setting = 0;
	}
	else
	{
		//fprintf(cgiOut, "open = %s\n",marking_setting);
		nMarking_setting = 1;
	}

	ret =  strstr(camera_HDMI_display,"middle");
	if(ret != NULL)
	{
		//fprintf(cgiOut, "nCamera_HDMI middle\n");
		nCamera_HDMI = 1;
	}

	ret =  strstr(camera_HDMI_display,"left");
	if(ret != NULL)
	{
		//fprintf(cgiOut, "nCamera_HDMIleft\n");
		nCamera_HDMI = 2;
	}

	ret =  strstr(camera_HDMI_display,"right");
	if(ret != NULL)
	{
		//fprintf(cgiOut, "nCamera_HDMI right\n");
		nCamera_HDMI = 3;
	}


	

        ret =  strstr(camera_HDMI_display_switch,"open");
        if(ret != NULL)
        {
                //fprintf(cgiOut, "open = %s\n",marking_setting);
                
		
		 ;//nCamera_HDMI = (0x10000| nCamera_HDMI);
        }
        else
        {
                //fprintf(cgiOut, "close = %s\n",marking_setting);
		 nCamera_HDMI =  0;//(0x00000| nCamera_HDMI);
                
        }









//	fprintf(cgiOut, "llllllllllllllllll = %d\n",nCamera_line_number+6);
//	fprintf(cgiOut, "llllllllllllllllll = %d\n",nCamera_line_number+6);
//	fprintf(cgiOut, "llllllllllllllllll = %d\n",nCamera_line_number+6);
//	fprintf(cgiOut, "llllllllllllllllll = %d\n",nCamera_line_number+6);


#ifdef board
	AXI_Lite_Write(SET_AXI_camera_line_number,nCamera_line_number);
	AXI_Lite_Write(SET_AXI_whitebalance,nWhitebalance);
	AXI_Lite_Write(SET_AXI_image_rectification,nimage_rectification);
	AXI_Lite_Write(SET_AXI_camera_fusion_degree,nCamera_fusion_degree);
	AXI_Lite_Write(SET_AXI_camera_brightness,nCamera_brightness);
	AXI_Lite_Write(SET_AXI_camera_delay,nCcamera_delay);
	AXI_Lite_Write(SET_AXI_triggerinput_1,nTriggerinput_1);
	AXI_Lite_Write(SET_AXI_triggerinput_2,nTriggerinput_2);
	AXI_Lite_Write(SET_AXI_marking_setting_h,nMarking_setting);
	AXI_Lite_Write(SET_AXI_marking_setting,nMarking_setting);
	AXI_Lite_Write(SET_AXI_marking_number,nMarking_number);
	AXI_Lite_Write(SET_AXI_HDMI,nCamera_HDMI);

#endif

	//刷新页面
	fprintf(cgiOut, "<meta http-equiv=Refresh content=0;URL=../manager.html\n");
	fprintf(cgiOut, "</BODY></HTML>\n");

#ifdef board
	//AXI_Lite_Close();
#endif
	return 0;
}

